<?php

namespace App\Utils\Push;

use Hedeqiang\UMeng\Facades\Push;

class UPush
{
    /**
     * 推送消息
     * @param $uid
     * @param $title
     * @param $message
     * @param array $extra
     * @return bool
     */
    public function send($uid, $title, $message, array $extra = []): bool
    {
        $response = $this->sendAndroid($uid, $title, $message, $extra);
        if ($response && $response['ret'] === 'SUCCESS')
            return true;
        else
            return false;
    }

    /**
     * 推送安卓消息
     * @param $uid
     * @param $title
     * @param $message
     * @param array $extra
     * @return array
     */
    private function sendAndroid($uid, $title, $message, array $extra = []): array
    {
        $params = [
            'type'          => 'customizedcast',
            'alias_type'    => 'uid',
            'alias'         => $uid,
            'payload'       => [
                'display_type' => 'notification',
                'body'      =>[
                    'title'         => $title,
                    'text'          => $message,
                    'after_open'    => 'go_activity',
                    'activity'      => 'com.bt.toysmodel.push.PushPopupActivity',
                    'custom'
                ],
                'extra'     => $extra,
                'channel_properties' => [
                    'channel_activity' => 'com.bt.toysmodel.push.PushPopupActivity',
                    'xiaomi_channel_id'=> '112635',
                    'vivo_category'    => 'ORDER',
                    'oppo_channel_id'  => 'Order',
                    'main_activity'    => 'com.bt.toysmodel.ui.LogoActivity',
                    'huawei_channel_importance' => 'NORMAL',
                    'huawei_channel_category' => 'EXPRESS',
                    'channel_fcm'      => 0
                ]
            ]
        ];
        echo json_encode($params, JSON_UNESCAPED_UNICODE);
        return Push::android()->send($params);
    }
}
